package com.suning.sawp.service.impl.oga.bi;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.oga.bi.DateDevelopDetail;
import com.suning.sawp.dto.oga.bi.DevelopInfo;
import com.suning.sawp.dto.oga.bi.SimpleRange;
import com.suning.sawp.dto.oga.bi.StaffDetailDevelopInfo;
import com.suning.sawp.dto.oga.bi.StaffDevelopInfo;
import com.suning.sawp.po.bi.BiDevelopInfo;
import com.suning.sawp.po.bi.BiStaffDevelop;
import com.suning.sawp.po.bi.BiStaffDevelopDayEntity;
import com.suning.sawp.po.bi.BiStoreDevelop;
import com.suning.sawp.service.impl.oga.bi.support.Populates;
import com.suning.sawp.service.impl.oga.bi.support.Callback;

/**
 * 数据统计服务
 * 
 * @author 13071602
 *
 */
public abstract class BIDevelopStatsService {

    @Autowired
    protected DalClient dalClient;

    /**
     * 查询门店拓展汇总
     * 
     * @param storeCode
     * @param offset
     * @return
     */
    public DevelopInfo queryStoreDevelopInfo(String storeCode, int offset) {
        DevelopInfo developInfo = new DevelopInfo();
        BiStoreDevelop storeDevelop = queryStoreDevelop(storeCode, offset);
        if (null == storeDevelop) {
            throw new IllegalStateException("暂无销售数据");
        }
        Populates.populateDevelopInfo(developInfo, storeDevelop);
        return developInfo;
    }

    /**
     * 店员拓展汇总
     * 
     * @param storeCode
     * @param cateCode
     * @param offset
     * @return
     */
    public StaffDetailDevelopInfo queryStaffDevelopInfo(final String storeCode, final String staffId, int offset) {
        final StaffDetailDevelopInfo staffDetailDevelopInfo = new StaffDetailDevelopInfo();
        BiDevelopInfo total = queryStaffDevelop(storeCode, staffId, offset, new Callback<SimpleRange<String>>() {
            @Override
            public void withValue(SimpleRange<String> range) {
                staffDetailDevelopInfo.setDetail(
                        queryPeriodDetailInfo(storeCode, staffId, range.getLowerBound(), range.getUpperBound()));

            }
        });
        if (null == total) {
            throw new IllegalArgumentException("暂无销售数据");
        }
        Populates.populateDevelopInfo(staffDetailDevelopInfo, total);
        return staffDetailDevelopInfo;
    }

    /**
     * 查询店员拓展详情
     * 
     * @param storeCode
     * @param staffId
     * @param offset
     * @return
     */
    protected abstract BiDevelopInfo queryStaffDevelop(String storeCode, String staffId, int offset,
            Callback<SimpleRange<String>> range);

    /**
     * 店员拓展详情时间范围
     * 
     * @return
     */
    public List<DateDevelopDetail> queryPeriodDetailInfo(String storeCode, String staffId, String startDate,
            String endDate) {
        List<BiStaffDevelopDayEntity> entities = queryPeriodDetail(storeCode, staffId, startDate, endDate);
        return Lists.transform(entities, new Function<BiStaffDevelopDayEntity, DateDevelopDetail>() {
            @Override
            public DateDevelopDetail apply(BiStaffDevelopDayEntity input) {
                DateDevelopDetail dateDevelopDetail = new DateDevelopDetail();

                Populates.populateDevelopInfo(dateDevelopDetail, input);
                dateDevelopDetail.setDate(input.getStatsDate());
                return dateDevelopDetail;
            }
        });
    }

    protected List<BiStaffDevelopDayEntity> queryPeriodDetail(String storeCode, String staffId, String startDate,
            String endDate) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("startDate", startDate);
        paramMap.put("endDate", endDate);
        paramMap.put("storeCode", storeCode);
        paramMap.put("staffId", staffId);
        return dalClient.queryForList("BIDevelopStatsService.queryPeriodDetail", paramMap,
                BiStaffDevelopDayEntity.class);
    }

    /**
     * 分品类查询拓展汇总
     * 
     * @param storeCode
     * @param cateCode
     * @param offset
     * @return
     */
    public List<StaffDevelopInfo> queryStoreCateDevelopInfo(String storeCode, String cateCode, int offset) {

        return Lists.transform(queryCateDevelop(storeCode, cateCode, offset),
                new Function<BiStaffDevelop, StaffDevelopInfo>() {
                    @Override
                    public StaffDevelopInfo apply(BiStaffDevelop input) {
                        StaffDevelopInfo staffDevelopInfo = new StaffDevelopInfo();
                        staffDevelopInfo.setArrive(input.getArriveNum());
                        staffDevelopInfo.setDeveloping(input.getDevelopingNum());
                        staffDevelopInfo.setFollow(input.getFollowNum());
                        staffDevelopInfo.setRecommend(input.getRecommendNum());
                        staffDevelopInfo.setStaffId(input.getStaffId());
                        staffDevelopInfo.setStaffName(input.getStaffName());
                        return staffDevelopInfo;
                    }
                });

    }

    protected abstract BiStoreDevelop queryStoreDevelop(String storeCode, int offset);

    protected abstract List<? extends BiStaffDevelop> queryCateDevelop(String storeCode, String cateCode, int offset);

}
